<?php

namespace App\Store\Services\Member;

use App\Store\Models\Member\Member as MemberMember;
use App\Store\Models\Member\SiteMemberLv;
use App\Store\Models\Sysset\Score;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Member\MemberPointsChangelog;

class CommonMember
{


    /**
     *  会员等级检查
     * @param $mem_id
     * @return true|void
     */
    public static function checkMemLv($mem_id)
    {
        $meminfo = MemberMember::where(['id' => $mem_id, 'is_del' => 1])->first();
        if (!empty($meminfo)) {
            //检查当前用户即将要升级的会员等级
            $current = SiteMemberLv::query()
                ->where([['cp_id', $meminfo->cp_id], ['init_points_num', '<=', $meminfo->points_total]])
                ->orderBy('init_points_num', 'desc')
                ->first();
            $current_id = $current->id ?? 0;

            // 如果没有会员等级但是有会员有积分则获取积分对应的会员等级
            MemberMember::query()
                ->where(['id' => $mem_id, 'is_del' => 1])
                ->update([
                    'member_lv' => $current_id,
                    'update_at' => time()
                ]);

//            if ((int)$meminfo->member_lv == 0 && $current_id > 0) {
//                // 如果没有会员等级但是有会员有积分则获取积分对应的会员等级
//                MemberMember::query()
//                    ->where(['id' => $mem_id, 'is_del' => 1])
//                    ->update([
//                        'member_lv' => $current_id,
//                        'update_at' => time()
//                    ]);
//            } else {
//                if ($current_id > 0 && ($meminfo->member_lv != $current_id)) {
//                    MemberMember::query()
//                        ->where(['id' => $mem_id, 'is_del' => 1])
//                        ->update([
//                            'member_lv' => $current_id,
//                            'update_at' => time()
//                        ]);
//                }
//            }
        }

        return true;

    }

    public static function setMemInitPoints(Request $request, $mem_id)
    {
        $meminfo = MemberMember::where(['id' => $mem_id, 'is_del' => 1])->first();
        if (!$meminfo) {
            return;
        }
        $s_value = Score::where(['cp_id' => $meminfo->cp_id, 'type' => 2, 'is_del' => 1])->orderBy('update_at', 'desc')->value('value');
        if ($s_value <= 0) {
            return;
        }
        MemberMember::where(['id' => $mem_id, 'is_del' => 1])->update(['points_total' => DB::raw('points_total + ' . $s_value), 'points_current' => DB::raw('points_current + ' . $s_value), 'update_at' => time()]);
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        if (isset($request->shopping_account_id1) && $request->shopping_account_id1 > 0) {
            $sales_account_id = $request->shopping_account_id1;
        } else {
            $sales_account_id = isset($request->shopping_account_id) ? intval($request->shopping_account_id) : 0;
        }
        $pointslogdata = [
            'cp_id' => $meminfo->cp_id,
            'mem_id' => $mem_id,
            'type' => 1,
            'store_id' => $meminfo->store_id,
            'change_val' => $s_value,
            'change_before' => $meminfo->points_current,
            'change_after' => $meminfo->points_current + $s_value,
            'sales_account_id' => $sales_account_id,
            'loginfo' => json_encode(['title' => '注册赠送']),
            'create_account_id' => $accountinfo['id'],
            'update_account_id' => $accountinfo['id'],
            'create_account_real_name' => $accountinfo['real_name'],
            'update_account_real_name' => $accountinfo['real_name'],
            'create_at' => time(),
            'update_at' => time(),
            'remark' => '注册赠送',
        ];
        MemberPointsChangelog::create($pointslogdata);

        return;
    }
}
